package com.zrwl.poscloud.core.restapi.mobile;

import com.zrwl.poscloud.base.constants.HomeworkBatchConstants;
import com.zrwl.poscloud.base.constants.HomeworkDetailConstants;
import com.zrwl.poscloud.commons.exception.ThrowableUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.validator.group.GetList;
import com.zrwl.poscloud.commons.validator.group.Insert;
import com.zrwl.poscloud.commons.validator.group.Update;
import com.zrwl.poscloud.core.annotion.AuthorityVerify.AuthorityVerify;
import com.zrwl.poscloud.core.annotion.OperationLogger.OperationLogger;
import com.zrwl.poscloud.xo.service.HomeworkBatchService;
import com.zrwl.poscloud.xo.service.HomeworkDetailService;
import com.zrwl.poscloud.xo.service.UserService;
import com.zrwl.poscloud.xo.vo.HomeworkBatchVO;
import com.zrwl.poscloud.xo.vo.HomeworkDetailVO;
import com.zrwl.poscloud.xo.vo.UserEmployeeRVO;
import com.zrwl.poscloud.xo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 作业管理 controller api类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-16
 */
@RestController
@Api(value = "作业管理相关接口", tags = {"作业管理相关接口"})
@RequestMapping("/userHomework")
@Slf4j
public class HomeworkRestApi {
    @Autowired
    private HomeworkDetailService homeworkDetailService;
    @Autowired
    private UserService userService;
    @AuthorityVerify
    @ApiOperation(value = "获取未提交的作业", notes = "获取未提交的作业", response = String.class)
    @PostMapping("/getNotFiledWorkList")
    public String getNotFiledWorkList(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取作业管理列表: {}", homeworkDetailVO);
        int [] status = {HomeworkDetailConstants.stay_receive,HomeworkDetailConstants.stay_submit};
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setBatchStatus(HomeworkBatchConstants.release);
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return ResultUtil.successWithData(homeworkDetailService.getNotFiledWorkList(homeworkDetailVO,status));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取作业列表", notes = "获取作业列表", response = String.class)
    @PostMapping("/getAllList")
    public String getAllList(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取作业列表: {}", homeworkDetailVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setBatchStatus(HomeworkBatchConstants.release);
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return ResultUtil.successWithData(homeworkDetailService.getAllList(homeworkDetailVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "获取作业列表", notes = "获取作业列表", response = String.class)
    @PostMapping("/getWorkDetailInfo")
    public String getWorkDetailInfo(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("获取作业列表: {}", homeworkDetailVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setBatchStatus(HomeworkBatchConstants.release);
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return ResultUtil.successWithData(homeworkDetailService.getWorkDetailInfo(homeworkDetailVO));
    }
    @AuthorityVerify
    @OperationLogger(value = "作业接收")    @ApiOperation(value = "作业接收", notes = "作业接收", response = String.class)
    @PostMapping("/editAccept")
    public String editAccept(@Validated({Update.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("作业接收: {}", homeworkDetailVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return homeworkDetailService.editAccept(homeworkDetailVO);
    }
    @AuthorityVerify
    @OperationLogger(value = "作业提交")    @ApiOperation(value = "作业提交", notes = "作业提交", response = String.class)
    @PostMapping("/addSubmit")
    public String addSubmit(@Validated({Insert.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("作业提交: {}", homeworkDetailVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return homeworkDetailService.addSubmit(homeworkDetailVO);
    }
    @AuthorityVerify
    @ApiOperation(value = "手机端分页获取作业列表", notes = "手机端分页获取作业列表", response = String.class)
    @PostMapping("/getHomeworkList")
    public String getHomeworkList(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("手机端分页获取作业列表: {}", homeworkDetailVO);
        UserVO userVO = userService.getMe();
        UserEmployeeRVO userEmployeeRVO = userVO.getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        homeworkDetailVO.setBatchStatus(HomeworkBatchConstants.release);
        homeworkDetailVO.setAcceptUserUid(userService.getUserWeChat(userVO.getWeChat()).getUid());
        return ResultUtil.successWithData(homeworkDetailService.getHomeworkList(homeworkDetailVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "手机端获取作业重复列表", notes = "手机端获取作业重复列表", response = String.class)
    @PostMapping("/getWorkRepeatDetailList")
    public String getWorkRepeatDetailList(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("手机端获取作业重复列表: {}", homeworkDetailVO);
        UserEmployeeRVO userEmployeeRVO = userService.getMe().getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        return ResultUtil.successWithData(homeworkDetailService.getWorkRepeatDetailList(homeworkDetailVO));
    }
    @AuthorityVerify
    @ApiOperation(value = "手机端获取第二次执行时间", notes = "手机端获取第二次执行时间", response = String.class)
    @PostMapping("/getSecondExecutionTime")
    public String getSecondExecutionTime(@Validated({GetList.class}) @RequestBody HomeworkDetailVO homeworkDetailVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        log.info("手机端获取第二次执行时间: {}", homeworkDetailVO);
        UserEmployeeRVO userEmployeeRVO = userService.getMe().getUserEmployeeRVO();
        homeworkDetailVO.setStudentUid(userEmployeeRVO.getEmployeeUid());
        return ResultUtil.successWithData(homeworkDetailService.getSecondExecutionTime(homeworkDetailVO));
    }
}
